/*
 * ProductInfo.java
 *
 * $Id: ProductInfo.java,v 1.1 2008-03-18 12:58:46 mario Exp $
 */
package org.ceteca.explica.core.traceability.tree;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * ValueObject with the data of a Product item.
 * <br/>
 * Responsibilities:
 * <ul>
 *  <li>Store the product data.</li>
 * </ul>
 * <br/>
 * @author Mario García García <mario@imagos.es>
 * Copyright (c) 2007 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ProductInfo {
	/** 
	 * Constant definition with the product source type NONE. 
	 * It's the default value to point out that no source type has been 
	 * assigned to the produt yet.
	 */
	public final static int SOURCE_NONE = 0x00;
	/**
	 * Constant definition with the product source type PURCHASE.
	 * It points out that the product has been acquired from an external
	 * supplier.
	 */
	public final static int SOURCE_PURCHASE = 0x01;
	/**
	 * Constant definition with the product source type PROCESS.
	 * It points out that the product has been produced from other 
	 * products as the result of an internal process.
	 */
	public final static int SOURCE_PROCESS = 0x02;
	
	/** Product set number */
	private String setNumber;
	/** Product description */
	private String description;
	/** Product barcode number */
	private String barcode;
	/** Product reference code */
	private String reference;
	/** Product in date (source purchase date or source process date) */
	private String dateIn;
	/** Product expiry date */
	private String dateExpiry;
	/** List of warehouses the product has been stored at */
	private ArrayList lWarehouses;
	/** List of comments linked to the product */
	private ArrayList lComments;
	/** List of extension properties of the product */
	private ArrayList lProperties;
	/** Product source process */
	private ProcessInfo sourceProcess;
	/** Product source purchase */
	private PurchaseInfo sourcePurchase;
	/** Product source type. It shows if the product has been acquired from
	 * a supplier (Purchase) or has been produced from other products as
	 * the result of an internal process (Process).
	 * @see org.ceteca.tree.ProductInfo#SOURCE_NONE
	 * @see org.ceteca.tree.ProductInfo#SOURCE_PROCESS
	 * @see org.ceteca.tree.ProductInfo#SOURCE_PURCHASE
	 */
	private int sourceType;
	/** Product traceability file path. In case the product is not the
	 * root product of the traceability file, i.e. the product is part
	 * of the input products of a process, the product may have a link
	 * to another traceability file where it's traceability is defined.
	 */
	private String traceabilityFile;
	
	/**
	 * Default ProductInfo constructor.
	 */
	public ProductInfo() {
		this.setReference(null);
		this.setSetNumber(null);
		this.setBarcode(null);
		this.setDescription(null);
		this.setDateIn(null);
		this.setDateExpiry(null);
		this.setTraceabilityFile(null);
		this.setSource((PurchaseInfo)null);
		this.setSourceType(SOURCE_NONE);
	}
	
	/**
	 * ProductInfo constructor that initializes the product values.
	 * @param reference String, product reference.
	 * @param setNumber String, product set number.
	 * @param barcode String, product barcode.
	 * @param description String, product description.
	 * @param dateIn String, product in date.
	 * @param dateExpiry String, product expiry date.
	 * @param traceabilityFile String, product traceability file path.
	 */
	public ProductInfo(String reference, String setNumber, String barcode, 
			String description, String dateIn, String dateExpiry, 
			String traceabilityFile) {
		this.setReference(reference);
		this.setSetNumber(setNumber);
		this.setBarcode(barcode);
		this.setDescription(description);
		this.setDateIn(dateIn);
		this.setDateExpiry(dateExpiry);
		this.setTraceabilityFile(traceabilityFile);
		this.setSource((PurchaseInfo)null);
		this.setSourceType(SOURCE_NONE);
	}
	
	/**
	 * Gets the product barcode number.
	 * @return String, product barcode number.
	 */
	public String getBarcode() {
		return barcode;
	}
	/**
	 * Sets up the product barcode number.
	 * @param barcode String, a barcode number.
	 */
	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}
	/**
	 * Gets the product in date.
	 * @return String, product in date.
	 */
	public String getDateIn() {
		return dateIn;
	}
	/**
	 * Sets up the product in date.
	 * @param dateIn String, a date.
	 */
	public void setDateIn(String dateIn) {
		this.dateIn = dateIn;
	}
	/**
	 * Gets the product expiry date.
	 * @return String, product expiry date.
	 */
	public String getDateExpiry() {
		return dateExpiry;
	}
	/**
	 * Sets up the product expiry date.
	 * @param dateExpiry String, a date.
	 */
	public void setDateExpiry(String dateExpiry) {
		this.dateExpiry = dateExpiry;
	}
	/**
	 * Gets the product description.
	 * @return String, product description.
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * Sets up the product description.
	 * @param description String, a description.
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/**
	 * Gets the product comments' list.
	 * @return ArrayList, list of product comments.
	 */
	public ArrayList getLComments() {
		return lComments;
	}
	/**
	 * Sets up the list of comments for the product.
	 * @param comments ArrayList, list of comments.
	 */
	public void setLComments(ArrayList comments) {
		lComments = comments;
	}
	/**
	 * Adds a new comment to the product comments' list.
	 * @param c String, comment's data to insert.
	 */
	public void addComment(String c) {
		if (lComments == null)
			lComments = new ArrayList();
		lComments.add(c);
	}
	/**
	 * Adds a list of comments to the product comments' list.
	 * @param comments ArrayList<String>, comments' list to insert.
	 */
	public void addComments(ArrayList<String> comments) {
		if (lComments == null)
			lComments = new ArrayList();
		lComments.addAll(comments);
	}
	/**
	 * Removes a comment from the product comments' list.
	 * @param c String, comment's data to delete.
	 */
	public void delComment(String c) {
		if (lComments != null && lComments.size() > 0) {
			lComments.remove(c);
		}
	}
	
	/**
	 * Gets the product warehouses' list.
	 * @return ArrayList, list of product warehouses.
	 */
	public ArrayList getLWarehouses() {
		return lWarehouses;
	}
	/**
	 * Sets up the list of warehouses for the product.
	 * @param warehouses ArrayList, list of warehouses.
	 */
	public void setLWarehouses(ArrayList warehouses) {
		lWarehouses = warehouses;
	}
	/**
	 * Adds a new warehouse to the product warehouses' list.
	 * @param w WarehouseInfo, warehouse's data to insert.
	 */
	public void addWarehouse(WarehouseInfo w) {
		if (lWarehouses == null)
			lWarehouses = new ArrayList();
		lWarehouses.add(w);
	}
	/**
	 * Removes a warehouse from the product warehouses' list.
	 * @param w WarehouseInfo, warehouse's data to delete.
	 */
	public void delWarehouse(WarehouseInfo w) {
		if (lWarehouses != null && lWarehouses.size() > 0) {
			lWarehouses.remove(w);
		}
	}
	
	/**
	 * Gets the product properties' list.
	 * @return ArrayList, list of product properties.
	 */
	public ArrayList getLProperties() {
		return lProperties;
	}
	/**
	 * Sets up the list of properties for the product.
	 * @param properties ArrayList, list of properties.
	 */
	public void setLProperties(ArrayList properties) {
		lProperties = properties;
	}
	/**
	 * Adds a new property to the product properties' list.
	 * @param p PropertyInfo, property's data to insert.
	 */
	public void addProperty(PropertyInfo p) {
		if (lProperties == null)
			lProperties = new ArrayList();
		lProperties.add(p);
	}
	/**
	 * Removes a property from the product properties' list.
	 * @param p PropertyInfo, property's data to delete.
	 */
	public void delProperty(PropertyInfo p) {
		if (lProperties != null && lProperties.size() > 0) {
			lProperties.remove(p);
		}
	}
	
	/**
	 * Gets the product reference.
	 * @return String, product reference.
	 */
	public String getReference() {
		return reference;
	}
	/**
	 * Sets up the product reference.
	 * @param reference String, a product reference.
	 */
	public void setReference(String reference) {
		this.reference = reference;
	}
	/**
	 * Gets the product set number.
	 * @return String, product set number.
	 */
	public String getSetNumber() {
		return setNumber;
	}
	/**
	 * Sets up the product set number.
	 * @param setNumber String, a product set number.
	 */
	public void setSetNumber(String setNumber) {
		this.setNumber = setNumber;
	}
	
	/**
	 * Gets the product source type.
	 * @return int, product source type.
	 * @see org.ceteca.explica.core.traceability.tree.ProductInfo#SOURCE_NONE
	 * @see org.ceteca.explica.core.traceability.tree.ProductInfo#SOURCE_PROCESS
	 * @see org.ceteca.explica.core.traceability.tree.ProductInfo#SOURCE_PURCHASE
	 */
	public int getSourceType() {
		return this.sourceType;
	}
	/**
	 * Sets up the product source type.
	 * @param sourceType int, a product source type.
	 * @see org.ceteca.explica.core.traceability.tree.ProductInfo#SOURCE_NONE
	 * @see org.ceteca.explica.core.traceability.tree.ProductInfo#SOURCE_PROCESS
	 * @see org.ceteca.explica.core.traceability.tree.ProductInfo#SOURCE_PURCHASE
	 */
	public void setSourceType(int sourceType) {
		this.sourceType = sourceType;
	}
	/**
	 * Sets up the product source of type Process.
	 * @param source ProcessInfo, a source process.
	 */
	public void setSource(ProcessInfo source) {
		this.setSourceProcess(source);
		this.setSourceType(SOURCE_PROCESS);
	}
	/**
	 * Sets up the product source of type Purchase.
	 * @param source PurchaseInfo, a source purchase.
	 */
	public void setSource(PurchaseInfo source) {
		this.setSourcePurchase(source);
		this.setSourceType(SOURCE_PURCHASE);
	}

	/**
	 * Gets the product source process.
	 * @return ProcessInfo, product source process.
	 */
	public ProcessInfo getSourceProcess() {
		return sourceProcess;
	}
	/**
	 * Sets up the product source of type Process.
	 * @param sourceProcess ProcessInfo, a source process.
	 */
	public void setSourceProcess(ProcessInfo sourceProcess) {
		this.sourceProcess = sourceProcess;
	}
	/**
	 * Gets the product source purchase.
	 * @return PurchaseInfo, product source purchase.
	 */
	public PurchaseInfo getSourcePurchase() {
		return sourcePurchase;
	}
	/**
	 * Sets up the product source of type Purchase.
	 * @param sourcePurchase PurchaseInfo, a source purchase.
	 */
	public void setSourcePurchase(PurchaseInfo sourcePurchase) {
		this.sourcePurchase = sourcePurchase;
	}

	/**
	 * Gets the product traceability file path.
	 * @return String, product traceability file path.
	 */
	public String getTraceabilityFile() {
		return traceabilityFile;
	}
	/**
	 * Sets up the product traceability file path.
	 * @param traceabilityFile String, a file path.
	 */
	public void setTraceabilityFile(String traceabilityFile) {
		this.traceabilityFile = traceabilityFile;
	}
	
	/**
	 * Converts the product info to a formatted String.
	 * @return String, formatted String with the product's data.
	 */
	public String toString() {
		StringBuffer result = new StringBuffer();
		result.append("PRODUCT No: ");
		result.append(this.reference);
		result.append("   (");
		result.append(this.setNumber);
		result.append(") --> ");
		result.append(this.description);
		result.append("\n");
		result.append("BARCODE = ");
		result.append(this.barcode);
		result.append("\n");
		result.append(this.dateIn);
		result.append("  -  ");
		result.append(this.dateExpiry);
		result.append("\n");
		
		switch (this.sourceType) {
			case SOURCE_PURCHASE:
				result.append("PURCHASE SOURCE: ");
				result.append(this.sourcePurchase);
				break;
			case SOURCE_PROCESS:
				result.append("PROCESS SOURCE: ");
				result.append(this.sourceProcess);
				break;
			case SOURCE_NONE:
				result.append("NO SOURCE:  ");
				break;
		}
		
		result.append("\n");
		result.append("{");
		result.append(this.traceabilityFile);
		result.append("}");
		
		if (this.lProperties != null) {
			result.append("\n");
			result.append("----------");
			result.append("\n");
			Iterator it = lProperties.iterator();
			while (it != null && it.hasNext()) {
				result.append("\n");
				result.append(it.next());
				result.append("\n");
				result.append("·$.$.$.$.$.$");
			}
			result.append("\n");
			result.append("----------");
		}
		
		if (this.lWarehouses != null) {
			result.append("\n");
			result.append("##########");
			result.append("\n");
			Iterator it = lWarehouses.iterator();
			while (it != null && it.hasNext()) {
				result.append("\n");
				result.append(it.next());
				result.append("\n");
				result.append("·$.$.$.$.$.$");
			}
			result.append("\n");
			result.append("##########");
		}
		
		if (this.lComments != null) {
			result.append("\n");
			result.append("-.-.-.-.-.");
			result.append("\n");
			Iterator it = lComments.iterator();
			while (it != null && it.hasNext()) {
				result.append("\n");
				result.append(it.next());
				result.append("\n");
				result.append("·$.$.$.$.$.$");
			}
			result.append("\n");
			result.append("-.-.-.-.-.");
		}
		
		return result.toString();
	}
	
	/**
	 * Updates the product data.
	 * @param info ProductInfo, new product data.
	 */
	public void update(ProductInfo info) {
		this.setReference(info.getReference());
		this.setSetNumber(info.getSetNumber());
		this.setDescription(info.getDescription());
		this.setBarcode(info.getBarcode());
		this.setDateIn(info.getDateIn());
		this.setDateExpiry(info.getDateExpiry());
		this.setTraceabilityFile(info.getTraceabilityFile());
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: ProductInfo.java,v 1.1 2008-03-18 12:58:46 mario Exp $";
	}
}
